package com.lyf.emall.controller;

import com.lyf.emall.bean.Admin;
import com.lyf.emall.bean.Page;
import com.lyf.emall.bean.ResponseResult;
import com.lyf.emall.bean.SysUser;
import com.lyf.emall.service.AdminService;
import com.lyf.emall.utils.JwtUtil;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.*;


import java.sql.Timestamp;

/**
 * @Description:
 * @Author Lfy
 * @Date 2022/4/12-15:05
 */
@RestController
public class AdminController {

    @Autowired
    AdminService adminService;

    @GetMapping("/admin")
    @PreAuthorize("hasAuthority('/admin/list')")
    public ResponseResult getAllAdmin(Page page){
        return adminService.getAllAdmin(page);
    }

    @GetMapping("/admin/repeat")
    @PreAuthorize("hasAuthority('/admin/repeat')")
    public ResponseResult isAdmin(@RequestParam("userName") String userName){
        return adminService.isAdmin(userName);
    }

    @PostMapping("/admin")
    @PreAuthorize("hasAuthority('/admin/add')")
    public ResponseResult addAdmin(@RequestParam("userName") String userName,
                                   @RequestParam("password") String password,
                                   @RequestParam("phoneNumber") String phoneNumber,
                                   @RequestParam("email") String email,
                                   @RequestParam("sex") String sex,
                                   @RequestParam("roleId") String roleId,
                                   @RequestParam("description") String description,
                                   @RequestHeader String token) throws Exception {

        // 解析token
        Claims claims = JwtUtil.parseJWT(token);
        String userId = claims.getSubject();

        // 密码加密
        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
        String encode = bCryptPasswordEncoder.encode(password);  // 加密后的密码

        Timestamp timestamp = new Timestamp(System.currentTimeMillis());
        Admin admin = new Admin(null, userName, encode, phoneNumber, email, sex, null, 1, 1, 1, null, description, timestamp, timestamp, Integer.valueOf(userId), null);

        return adminService.addAdmin(admin, Integer.valueOf(roleId));
    }

    @PutMapping("/admin/password")
    @PreAuthorize("hasAuthority('/admin/password')")
    public ResponseResult updatePassword(@RequestParam("userId") String userId,
                                         @RequestParam("password") String password){
        // 密码加密
        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
        String encode = bCryptPasswordEncoder.encode(password);  // 加密后的密码

        return adminService.updatePassword(new SysUser(Integer.valueOf(userId), encode));
    }

    @PostMapping("/admin/status")
    @PreAuthorize("hasAuthority('/admin/status')")
    public ResponseResult updateAdminStatus(@RequestParam("userId") String userId,
                                            @RequestParam("status") String status){
        return adminService.updateStatus(Integer.valueOf(userId), Integer.valueOf(status));
    }

    @DeleteMapping("/admin")
    @PreAuthorize("hasAuthority('/admin/delete')")
    public ResponseResult deleteAdmin(@RequestParam("userId") String useId){
        return adminService.deleteAdmin(Integer.valueOf(useId));
    }

    @PutMapping("/admin")
    @PreAuthorize("hasAuthority('/admin/put')")
    public ResponseResult updateAdmin(@RequestParam("userId") String userId,
                                      @RequestParam("phoneNumber") String phoneNumber,
                                      @RequestParam("email") String email,
                                      @RequestParam("sex") String sex,
                                      @RequestParam("roleId") String roleId,
                                      @RequestParam("description") String description,
                                      @RequestHeader String token) throws Exception {

        // 解析token
        Claims claims = JwtUtil.parseJWT(token);
        String updateId = claims.getSubject();

        Timestamp timestamp = new Timestamp(System.currentTimeMillis());
        SysUser user = new SysUser(Integer.valueOf(userId), null, null, phoneNumber, email, sex, null, null, null, null, null, description, null, timestamp, Integer.valueOf(updateId), null);

        return adminService.updateAdmin(user, Integer.valueOf(roleId));
    }
}
